package com.iraeta.common;

import com.github.pagehelper.PageHelper;
import com.zaxxer.hikari.HikariDataSource;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;
import java.util.Properties;

@Configuration
@MapperScan(basePackages = "com.iraeta.dao.main", sqlSessionFactoryRef = "mainSqlSessionFactory")
public class DataSourceConfigMain {

    /**
     * 指定mapper xml文件路径
     */
    public static final String MAPPER_LOCATION = "classpath*:/mapper/main/*.xml";

    @Primary // 表示这个数据源是默认数据源, 这个注解必须要加，因为不加的话spring将分不清楚那个为主数据源（默认数据源）
    @Bean("mainDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.main") //读取application.yml中的配置参数映射成为一个对象
    public DataSource getMainDataSource(){
        return DataSourceBuilder.create().type(HikariDataSource.class).build();
    }

    @Primary
    @Bean("mainSqlSessionFactory")
    public SqlSessionFactory mainSqlSessionFactory(@Qualifier("mainDataSource") DataSource dataSource) throws Exception {
        final SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        // mapper的xml形式文件位置必须要配置，不然将报错：no statement （这种错误也可能是mapper的xml中，namespace与项目的路径不一致导致）
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(MAPPER_LOCATION));
        //设置分页插件
        Interceptor interceptor = new PageHelper();
        Properties p = new Properties();
        p.setProperty("dialect", "Mysql");
        p.setProperty("offsetAsPageNum", "true");
        p.setProperty("rowBoundsWithCount", "true");
        interceptor.setProperties(p);
        bean.setPlugins(interceptor);
        return bean.getObject();
    }

    @Primary
    @Bean("mainSqlSessionTemplate")
    public SqlSessionTemplate mainSqlSessionTemplate(@Qualifier("mainSqlSessionFactory") SqlSessionFactory sqlSessionFactory){
        return new SqlSessionTemplate(sqlSessionFactory);
    }


    /*https://blog.csdn.net/qq_17229141/article/details/128408860*/
//    @Primary
//    @ConfigurationProperties("spring.datasource.main")
//    public DataSourceProperties mainDataSourceProperties() {
//        return new DataSourceProperties();
//    }
//    @Primary
//    @Bean("mainDataSource")
//    @Qualifier(value = "mainDataSource")
//    @ConfigurationProperties(prefix = "spring.datasource.main.hikari")
//    public HikariDataSource mainDataSource() {
//        return mainDataSourceProperties().initializeDataSourceBuilder().type(HikariDataSource.class).build();
//    }
//    @Bean(name = "mainTransactionManager")
//    @Primary
//    public PlatformTransactionManager mainTransactionManager(@Qualifier("mainDataSource") DataSource dataSource) {
//        return new DataSourceTransactionManager(dataSource);
//    }
//    @Primary
//    @Bean(name = "mainSqlSessionFactory")
//    public SqlSessionFactory mainSqlSessionFactory(@Qualifier("mainDataSource") DataSource dataSource) throws Exception {
//        final SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();
//        sessionFactoryBean.setDataSource(dataSource);
//        sessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(DataSourceConfigMain.MAPPER_LOCATION));
//        return sessionFactoryBean.getObject();
//    }
//    @Bean("mainSqlSessionTemplate")
//    public SqlSessionTemplate mainSqlSessionTemplate(@Qualifier("mainSqlSessionFactory") SqlSessionFactory sqlSessionFactory){
//        return new SqlSessionTemplate(sqlSessionFactory);
//    }
}
